package cn.zyq.ykb.service.customer.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.customer.CustomerMapper;
import cn.zyq.ykb.beans.customer.Customer;
import cn.zyq.ykb.service.customer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 客服电话Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-22
 */
@Service
public class CustomerServiceImpl  extends BaseService implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询客服电话
     *
     * @param customerID 客服电话主键
     * @return 客服电话
     */
    @Override
    public Customer selectCustomerByCustomerID(Integer customerID) {
        Customer customer = customerMapper.selectByPrimaryKey(customerID);
        if (customer.getDeleted() == 1) {
            return null;
        }
        return customer;
    }

    /**
     * 查询客服电话
     *
     * @param serviceID 业务ID
     * @return 客服电话
     */
    @Override
    public Customer selectCustomerByServiceID(String serviceID) {
        Customer param = new Customer();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.customerMapper.selectOne(param);
    }

    /**
     * 查询客服电话列表
     *
     * @param pageParam
     * @return 客服电话
     */
    @Override
    public PageInfo<Customer> selectCustomerList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Customer> list = this.customerMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询客服电话列表
     *
     * @param param
     * @return 客服电话
     */
    @Override
    public List<Customer> selectCustomerList(Customer param) {
        param.setDeleted(0);
        return this.customerMapper.select(param);
    }
    /**
     * 新增客服电话
     *
     * @param param 客服电话
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.customerMapper.insertSelective(param);
    }

    /**
     * 修改客服电话
     *
     * @param param 客服电话
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.customerMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除客服电话
     *
     * @param customerIDs 需要删除的客服电话主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustomerIDs(Integer[] customerIDs) {
        int i = 0;
        for (Integer customerID : customerIDs) {
            i += this.deleteCustomerByCustomerID(customerID);
        }
        return i;
    }

    /**
     * 删除客服电话信息
     *
     * @param customerID 客服电话主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustomerID(Integer customerID) {
        Customer customer = this.selectCustomerByCustomerID(customerID);
        if (customer == null) {
            return 0;
        } else {
            Customer storeInfo = new Customer();
            storeInfo.setCustomerID(customerID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.customerMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
